//
//  PNWaitingView.m
//  PNCMobileBank
//
//  Created by 杨挺 on 2018/10/8.
//  Copyright © 2018年 杨挺. All rights reserved.
//

#import "PNWaitingView.h"
#import <objc/runtime.h>
#import "Masonry.h"

static NSString *const viewIdentifier = @"PNWaitingView_viewIdentifier";

@interface PNWaitingView ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation PNWaitingView


+ (void)showLoadingView {
    [self hideLoadingView];
//    PNWaitingView *view = objc_getAssociatedObject([UIApplication sharedApplication].keyWindow, viewIdentifier.UTF8String);
//    if (view.superview) {
//        [view.superview bringSubviewToFront:view];
//        return ;
//    }
    
    PNWaitingView *view = [[NSBundle mainBundle] loadNibNamed:@"PNWaitingView" owner:nil options:nil].lastObject;
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    objc_setAssociatedObject([UIApplication sharedApplication].keyWindow, viewIdentifier.UTF8String, view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(view.superview);
    }];
    NSArray *imageS = @[[UIImage imageNamed:@"loading1.jpg"],
                        [UIImage imageNamed:@"loading2.jpg"],
                        [UIImage imageNamed:@"loading3.jpg"]];
    view.imageView.animationImages = imageS;
    view.imageView.animationDuration = 1;
    view.imageView.animationRepeatCount = NSIntegerMax;
    [view.imageView startAnimating];

}

+ (void)hideLoadingView {
    PNWaitingView *view = objc_getAssociatedObject([UIApplication sharedApplication].keyWindow, viewIdentifier.UTF8String);
    if (view.superview) {
        [view removeFromSuperview];
        objc_setAssociatedObject([UIApplication sharedApplication].keyWindow, viewIdentifier.UTF8String, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}

@end
